//
//  UnarmedTodayCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/30.
//

import Foundation
import UIKit

class UnarmedTodayCell: UITableViewCell {
    let titleL = UILabel()
    let contentL = UILabel()
    let countL = UILabel()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: AlarmCbfqyCountModal?) {
//        switch modal.type {
//        case .WorkNet:
//            iconIV.image = UIImage(named: "ic_unarm_1")
//            break
//        case .ATMs:
//            iconIV.image = UIImage(named: "ic_unarm_2")
//            break
//        case .SelfHelp:
//            iconIV.image = UIImage(named: "ic_unarm_3")
//            break
//        case .OfficeArea:
//            iconIV.image = UIImage(named: "ic_unarm_4")
//            break
//        case .Treasury:
//            iconIV.image = UIImage(named: "ic_unarm_5")
//            break
//        }
//
//        titleL.text = modal.name
//        numL.text = "\(modal?.bfTotal ?? 0)/\(modal?.total ?? 0)"

        titleL.text = modal?.dictLabel

        switch UnarmedTodayType(rawValue: modal?.dictValue ?? "") {
        case .WorkNet:
            iconIV.image = UIImage(named: "ic_alarmnet_1")
        case .ATMs:
            iconIV.image = UIImage(named: "ic_alarmnet_2")
        case .SelfHelp:
            iconIV.image = UIImage(named: "ic_alarmnet_3")
        case .OfficeArea:
            iconIV.image = UIImage(named: "ic_alarmnet_4")
        case .Treasury:
            iconIV.image = UIImage(named: "ic_alarmnet_5")
        default:  break
        }
        
        let value = "\(modal?.bfTotal ?? 0)"
        let text = "\(modal?.bfTotal ?? 0) / \(modal?.total ?? 0)"
        let aAttri = NSMutableAttributedString(string: text, attributes: [.font:  UIFont.systemFont(ofSize: 15), .foregroundColor: UIColor.hex("#333333")])
        let aRange = NSMakeRange(text.distance(from: text.startIndex, to:text.range(of: value)!.lowerBound), value.count)
        aAttri.addAttribute(.font, value: UIFont.systemFont(ofSize: 15), range: aRange)
        aAttri.addAttribute(.foregroundColor, value: UIColor.hex("#FF0000"), range: aRange)
        countL.attributedText = aAttri
    }
    
    func setupUI() {
        iconIV.image = UIImage()
        iconIV.contentMode = .center
        addSubview(iconIV)
        iconIV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(20)
        }
        
        titleL.textColor = .hex("#333333")
        titleL.font = .systemFont(ofSize: 15)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconIV.snp.right).offset(16)
        }
        
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
        countL.textColor = .hex("#333333")
        countL.font = .systemFont(ofSize: 15)
        countL.textAlignment = .right
        addSubview(countL)
        countL.snp.makeConstraints { make in
            make.centerY.equalTo(titleL.snp.centerY)
            make.right.equalTo(arrowIV.snp.left).offset(-28)
        }
        
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.bottom.equalToSuperview()
            make.left.equalTo(iconIV.snp.left)
            make.right.equalTo(arrowIV.snp.right)
            make.height.equalTo(0.5)
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)
        if highlighted {
            backgroundColor = .hex("#ECECEC")
        } else {
            backgroundColor = .white
        }
    }
    lazy var iconIV: UIImageView = {
        let iv = UIImageView()
        return iv
    }()
    
    lazy var arrowIV: UIImageView = {
        let iv = UIImageView(image: UIImage(systemName: "chevron.right"))
        iv.tintColor = .hex("#A5A5A5")
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    lazy var line: UIView = {
        let li = UIView()
        li.backgroundColor = .hex("#F3F3F3")
        return li
    }()
}


